let migong = new Array(8);
let res = new Array(8);
let sum = 0;

twoArr(migong); //生成二维数组
twoArr(res);
init(migong); //初始二维数组
migong[3][1] = 1; //设置障碍
migong[3][2] = 1;
migong[6][6] = 2; //设置出口

console.log(migong)
searchWax(migong, 1, 1);
console.log(migong)
function searchWax(arr, y, x){
  if(arr[y][x] === 2){    //出口
    return true;
  } else {
    if(arr[y][x] === 0){
      arr[y][x] = 2;
      if(searchWax(arr, y + 1, x)){  //先向下走
        return true;
      } else if(searchWax(arr, y, x + 1)){   //向右走 
        
        return true; 
      } else if(searchWax(arr, y - 1, x)){    //向上走
       
        return true;
      } else if(searchWax(arr, y, x - 1)){  //向左走
        
        return true;
      } else {
        return false;
      }
    } else {
      arr[y][x] = 3;//标记
      return false;
    }
  }
  
}

function twoArr(arr){
  for(let i = 0; i<arr.length; i++){
    arr[i] = new Array(8);
  }
}

function init(arr){
  for(let i = 0; i<arr.length; i++){
    for(let j = 0; j<arr[i].length; j++){
      if(i === 0 || i === arr.length - 1){
        arr[i][j] = 1;
      } else if(j === 0 || j === arr[i].length - 1){
        arr[i][j] = 1;
      } else {
        arr[i][j] = 0;
      }
    }
  }
}